using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Collections.Generic;
using PigeonCms;

public partial class Controls_Default : PigeonCms.BaseModuleControl
{
    protected string ModuleFullName
    {
        get { return base.GetStringParam("ModuleFullName", "", "ModuleFullName"); }
    }

    protected new void Page_Init(object sender, EventArgs e)
    {
        base.Page_Init(sender, e);

        foreach (KeyValuePair<string, string> item in Config.CultureList)
        {
            //value
            var txtValue = new TextBox();
            txtValue.ID = "TxtValue" + item.Value;
            txtValue.CssClass = "adminText";
            //txtValue.TextMode = TextBoxMode.MultiLine;
            txtValue.Rows = 2;
            PanelValue.Controls.Add(txtValue);
            var litValue = new Literal();
            litValue.Text = "&nbsp;[<i>" + item.Value + "</i>]<br /><br />";
            PanelValue.Controls.Add(litValue);

            //comment
            var txtComment = new TextBox();
            txtComment.ID = "TxtComment" + item.Value;
            txtComment.CssClass = "adminText";
            //txtComment.TextMode = TextBoxMode.MultiLine;
            txtComment.Rows = 2;
            PanelComment.Controls.Add(txtComment);
            var litComment = new Literal();
            litComment.Text = "&nbsp;[<i>" + item.Value + "</i>]<br /><br />";
            PanelComment.Controls.Add(litComment);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LblOk.Text = "";
        LblErr.Text = "";

        if (!Page.IsPostBack)
        {
            loadDropsModuleTypes();
        }
    }

    protected void DropModuleTypesFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        Grid1.DataBind();
    }


    protected void ObjDs1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        var filter = new LabelTransFilter();

        if (!string.IsNullOrEmpty(this.ModuleFullName))
            filter.ResourceSet = this.ModuleFullName;
        else
            filter.ResourceSet = DropModuleTypesFilter.SelectedValue;

        e.InputParameters["filter"] = filter;
    }

    protected void Grid1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            editRow(e.CommandArgument.ToString());
        }
        if (e.CommandName == "DeleteRow")
        {
            deleteRow(e.CommandArgument.ToString());
        }
    }

    protected void Grid1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
            Utility.AddGlyph(Grid1, e.Row);
    }

    protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var item = new ResLabelTrans();
            item = (ResLabelTrans)e.Row.DataItem;

            Literal LitValue = (Literal)e.Row.FindControl("LitValue");
            string values = "";

            var labelList = new List<ResLabel>();
            var man = new LabelsManager();
            var lfilter = new LabelsFilter();
            lfilter.ResourceSet = item.ResourceSet;
            lfilter.ResourceId = item.ResourceId;
            labelList = man.GetByFilter(lfilter, "");
            foreach(var label in labelList)
            {
                values += label.Value + ", ";                
            }
            if (values.EndsWith(", ")) values = values.Substring(0, values.Length - 2);
            LitValue.Text += Utility.Html.GetTextPreview(values, 100, "");
        }
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (saveForm())
            MultiView1.ActiveViewIndex = 0;
    }

    protected void BtnNew_Click(object sender, EventArgs e)
    {
        LblErr.Text = "";
        if (DropModuleTypesFilter.SelectedValue != "xxx")
            editRow("");
    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        LblErr.Text = "";
        LblOk.Text = "";
        MultiView1.ActiveViewIndex = 0;
    }

    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 0)    //list view
        {
        }

        if (MultiView1.ActiveViewIndex == 1)    //edit view
        {
        }
    }

    #region private methods
    private void clearForm()
    {
        LitResourceSet.Text = "";
        TxtResourceId.Text = "";
        foreach (KeyValuePair<string, string> item in Config.CultureList)
        {
            var TxtValue = new TextBox();
            TxtValue = (TextBox)PanelValue.FindControl("TxtValue" + item.Value);
            TxtValue.Text = "";

            var TxtComment = new TextBox();
            TxtComment = (TextBox)PanelComment.FindControl("TxtComment" + item.Value);
            TxtComment.Text = "";
        }
    }

    private void form2obj(ResLabelTrans obj)
    {        
        obj.ResourceSet = LitResourceSet.Text;
        obj.ResourceId = TxtResourceId.Text;
    }

    private void obj2form(ResLabelTrans obj)
    {
        var label = new ResLabel();
        var labelsList = new List<ResLabel>();
        var filter = new LabelsFilter();
        var lman = new LabelsManager();

        LitResourceSet.Text = DropModuleTypesFilter.SelectedValue;
        TxtResourceId.Text = obj.ResourceId;
        TxtResourceId.Enabled = true;

        filter.ResourceSet = DropModuleTypesFilter.SelectedValue;
        filter.ResourceId = obj.ResourceId;

        if (!string.IsNullOrEmpty(obj.ResourceId))
        {
            TxtResourceId.Enabled = false;
            foreach (KeyValuePair<string, string> item in Config.CultureList)
            {
                label = new ResLabel();
                var TxtValue = new TextBox();
                var TxtComment = new TextBox();
                TxtValue = (TextBox)PanelValue.FindControl("TxtValue" + item.Value);
                TxtComment = (TextBox)PanelComment.FindControl("TxtComment" + item.Value);

                filter.CultureName = item.Key;
                labelsList = lman.GetByFilter(filter, "");
                if (labelsList.Count > 0)
                    label = labelsList[0];
                TxtValue.Text = label.Value;
                TxtComment.Text = label.Comment;
            }
        }
    }

    private void editRow(string resourceId)
    {
        var obj = new PigeonCms.ResLabelTrans();
        LblOk.Text = "";
        LblErr.Text = "";

        clearForm();
        CurrentKey = resourceId;
        if (string.IsNullOrEmpty(resourceId))
        {
            obj2form(obj);
        }
        else
        {
            obj = new LabelsManager().GetLabelTransByKey(DropModuleTypesFilter.SelectedValue, resourceId);
            obj2form(obj);
        }
        MultiView1.ActiveViewIndex = 1;
    }

    private bool saveForm()
    {
        bool res = false;
        var man = new LabelsManager();
        var lFilter = new LabelsFilter();
        LblErr.Text = "";
        LblOk.Text = "";

        try
        {
            var o1 = new ResLabelTrans();
            o1 = man.GetLabelTransByKey(DropModuleTypesFilter.SelectedValue, CurrentKey);
            form2obj(o1);            
            foreach (KeyValuePair<string, string> item in Config.CultureList)
            {
                var label = new ResLabel();
                var labelList = new List<ResLabel>();

                TextBox TxtValue = new TextBox();
                TxtValue = (TextBox)PanelValue.FindControl("TxtValue" + item.Value);
                TextBox TxtComment = new TextBox();
                TxtComment = (TextBox)PanelComment.FindControl("TxtComment" + item.Value);

                //man.DeleteByResourceId(o1.ResourceSet, o1.ResourceId, item.Key);

                lFilter.ResourceSet = o1.ResourceSet;
                lFilter.ResourceId = o1.ResourceId;
                lFilter.CultureName = item.Key;
                labelList = man.GetByFilter(lFilter, "");
                if (labelList.Count > 0)
                {
                    label = labelList[0];
                    label.Value = TxtValue.Text;
                    label.Comment = TxtComment.Text;
                    man.Update(label);
                }
                else
                {
                    label.ResourceSet = o1.ResourceSet;
                    label.ResourceId = o1.ResourceId;
                    label.CultureName = item.Key;
                    label.Value = TxtValue.Text;
                    label.Comment = TxtComment.Text;
                    man.Insert(label);
                }
            }
            LabelsProvider.ClearCacheByResourceSet(o1.ResourceSet);
            Grid1.DataBind();
            LblOk.Text = Utility.GetLabel("RECORD_SAVED_MSG");
            MultiView1.ActiveViewIndex = 0;
            res = true;
        }
        catch (Exception e1)
        {
            LblErr.Text = Utility.GetLabel("RECORD_ERR_MSG") + "<br />" + e1.ToString();
        }
        finally
        { }
        return res;
    }

    private void deleteRow(string resourceId)
    {
        LblOk.Text = "";
        LblErr.Text = "";

        try
        {
            new LabelsManager().DeleteByResourceId(DropModuleTypesFilter.SelectedValue, resourceId);
        }
        catch (Exception e)
        {
            LblErr.Text = e.Message;
        }
        Grid1.DataBind();
    }

    private void loadDropsModuleTypes()
    {
        try
        {
            DropModuleTypesFilter.Items.Clear();
            if (!string.IsNullOrEmpty(this.ModuleFullName))
            {
                DropModuleTypesFilter.Items.Add(
                    new ListItem(this.ModuleFullName, this.ModuleFullName));
            }
            else
            {
                DropModuleTypesFilter.Items.Add(
                    new ListItem("-- " + Utility.GetLabel("LblSelectModule", "Select module") + " --", "xxx"));
                Dictionary<string, string> recordList = new ModuleTypeManager(true).GetList();
                foreach (var record1 in recordList)
                {
                    DropModuleTypesFilter.Items.Add(
                        new ListItem(record1.Key, record1.Value));
                }

                recordList.Clear();
                DropModuleTypesFilter.Items.Add(
                    new ListItem("-- " + Utility.GetLabel("LblSelectControl", "Select control") + " --", "xxx"));
                recordList = new ControlTypeManager().GetList();
                foreach (var record1 in recordList)
                {
                    DropModuleTypesFilter.Items.Add(
                        new ListItem(record1.Key, record1.Value));
                }
            }
        }
        catch (Exception ex)
        {
            LblErr.Text = ex.ToString();
        }
    }

    #endregion
}
